package component

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntityManager"
)

type redisEntityService struct {
}

func (e *redisEntityService) QueryNotify(entityType string) []*EntityChangedNotifyVO {
	voList := make([]*EntityChangedNotifyVO, 0)

	redisService := commEntityManager.GetAgileService(entityType)

	// 装载数据：从redis读取数据，并获知变化状态
	addMap := make(map[string]commEntity.IEntity)
	delSet := make(map[string]commEntity.IEntity)
	mdyMap := make(map[string]commEntity.IEntity)
	redisService.LoadChangeEntities(&addMap, &delSet, &mdyMap)

	// 检测：数据
	if len(addMap) == 0 && len(delSet) == 0 && len(mdyMap) == 0 {
		return voList
	}

	for key, entity := range addMap {
		vo := &EntityChangedNotifyVO{}
		vo.Method = "insert"
		vo.Key = key
		vo.Entity = entity

		voList = append(voList, vo)
	}

	for key, entity := range mdyMap {
		vo := &EntityChangedNotifyVO{}
		vo.Method = "update"
		vo.Key = key
		vo.Entity = entity

		voList = append(voList, vo)
	}

	for key, _ := range mdyMap {
		vo := &EntityChangedNotifyVO{}
		vo.Method = "delete"
		vo.Key = key
		vo.Entity = nil

		voList = append(voList, vo)
	}

	return voList
}
